package com.unicom.si.security.utils;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component("encode")
public class CustomPasswordEncoder implements PasswordEncoder {

    private final String salt = "xu";

    // 加密
    public String encode(CharSequence rawPassword) {
        Md5PasswordEncoder encoder = new Md5PasswordEncoder();
        return encoder.encodePassword(rawPassword.toString(), salt);
    }

    // 匹配是否一致
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        Md5PasswordEncoder encoder = new Md5PasswordEncoder();
        return encoder.isPasswordValid(encodedPassword, rawPassword.toString(), salt);
    }

    public static void main(String[] args) {
        CustomPasswordEncoder encoder = new CustomPasswordEncoder();
        String encodeString = encoder.encode("admin");
        System.out.println("===========" + encodeString);

        boolean isMatch = encoder.matches("password", "dc8ecb90523bc45a3d40f3c5bf507a53");
        System.out.println("++++++++" + isMatch);
    }
}
